﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog.Extensions.Logging;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http.Extensions;

namespace UL.Filter
{
    public class Filters : IAsyncActionFilter
    {
        private readonly ILogger<Filters> logger1;

        public Filters(ILogger<Filters> logger1)
        {
            this.logger1 = logger1;
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //StopWatch这个类是一个秒表，可以统计所用的时间
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //开始计时 
            //要做的事情
            next(); // 调用Action方法
            sw.Stop(); //停止计时
            string n = sw.ElapsedMilliseconds.ToString(); //所用的毫秒数
            //还可以记录其他信息 
            //略 
            //记录Nlog日志
            logger1.LogInformation("方法执行时间为：{0}毫秒",n);
        }
    }

}
